data segment
	x       db   0ah
	y       db   10h
	mess1   db   'please input a suitable number,end input with enter!','$'
	mess2   db   'end input with enter','$'
	mess3   db   'choose what you want to change,10to16 or 16to10:','$'
	mess4   db   '1:demical to hex ','$'
	mess5   db   '2:hex to demical','$'
	mess6   db   'please input a hex number,end input with enter!','$'
data ends

code segment
main proc far
	assume cs:code,ds:data
start:     
	push ds
	sub  ax,ax
	push ax
	mov ax,data
	mov ds,ax

choose:
	lea dx,mess3          	;message show
	mov ah,9h
	int 21h

	mov dl,0dh	       
	mov ah,2h
	int 21h

	mov dl,0ah
	mov ah,2
	int 21h

	lea dx,mess4		;message show
	mov ah,9
	int 21h

	mov dl,0dh
	mov ah,2
	int 21h

	mov dl,0ah
	mov ah,2
	int 21h

	lea dx,mess5		;message show
	mov ah,9
	int 21h

	mov dl,0dh
	mov ah,2
	int 21h

	mov dl,0ah
	mov ah,2
	int 21h

	mov ah,1
	int 21h

        cmp al,31h          	
        jz  first

	cmp al,32h
        jz  second

first:
	call dehex		;demical to hex
	jmp exit
second: 
	call hexde    		;hex to demical 
exit: 
	mov dl,0dh
	mov ah,2
	int 21h

	mov dl,0ah
	mov ah,2
	int 21h

	ret
main endp



dehex proc near			;demical to hex
	mov dl,0dh
	mov ah,2
	int 21h

	mov dl,0ah
	mov ah,2
	int 21h

	mov bx,0
	mov ax,0

	lea dx,mess1		;message show
	mov ah,09
	int 21h

	mov dl,0dh
	mov ah,2
	int 21h

	mov dl,0ah
	mov ah,2
	int 21h
          
	lea dx,mess2		;message show
	mov ah,09
	int 21h

	mov dl,0dh
	mov ah,2
	int 21h

	mov dl,0ah
	mov ah,2
	int 21h

accept:				;accept data
	mov ax,0
	mov ah,1
	int 21h			

	sub al,30h
	cbw
	xchg ax,bx
	
	cmp bx,0
	jl  outer1
	
	cmp bx,0ah
	jnl outer1

	mov cx,10d
	mul cx
	add ax,bx
	xchg bx,ax
	loop accept

outer1:
	mov bx,4
	mov dh,0
outer:				;compute and output
	mov  cl,4
	rol  ax,cl
	mov cx,ax
	and al,0fh
	add al,30h
	cmp al,3ah
	jl  output 
	add al,07h
output:
	cmp al,30h
	jnz tag
	cmp dh,0
	jz zero
tag:	mov  dh,1
	mov  dl,al
	mov  ah,2
	int  21h
zero:
	mov  ax,cx
	dec  bx
	jz  exit0
	jmp outer
exit0:
	ret
dehex endp


hexde proc near			;hex to demical
	mov dl,0dh
	mov ah,2
	int 21h

	mov dl,0ah
	mov ah,2
	int 21h

	lea dx,mess6		;message show
	mov ah,09
	int 21h

	mov dl,0dh
	mov ah,2
	int 21h

	mov dl,0ah
	mov ah,2
	int 21h

	mov ax,0
	mov dx,0
receive:			;accept data
	mov ah,1
	int 21h

	sub al,30h
	cmp al,0
	jl  dis
	cmp al,0ah
	jnl compare1
	jmp figure

compare1:
	sub al,7h
	cmp al,0ah
	jl  dis
        cmp al,10h
	jnl compare2
	jmp figure

compare2:
	sub al,20h
	cmp al,0ah
	jl  dis
        cmp al,10h
	jnl dis

figure:			 
	mov ah,0
	xchg ax,dx
	mul y				
	xchg dx,ax
	add dx,ax
	jmp receive
dis:      			;compute and output
	mov ax,dx
	cwd
	mov dx,0
	mov cx,10000d
	div cx
	mov bx,dx
	mov dl,al
	mov si,0
	cmp dl,0h
	jnz tag1
	cmp si,0
	jz zero1

tag1:	
	mov si,1
	add dl,30h
	mov ah,2
	int 21h
zero1:
	mov ax,bx
	mov dx,0
	mov cx,1000d
	div cx
	mov bx,dx
	mov dl,al
	cmp dl,0h
	jnz tag2
	cmp si,0
	jz zero2

tag2:	
	mov si,1
	add dl,30h
	mov ah,2
	int 21h
zero2:
	mov ax,bx
	mov dx,0
	mov cx,100d
	div cx
	mov bx,dx
	mov dl,al
	cmp dl,0h

	jnz tag3
	cmp si,0
	jz zero3

tag3:	
	mov si,1
	add dl,30h
	mov ah,2
	int 21h
zero3:
	mov ax,bx
	mov dx,0
	mov cx,10d
	div cx
	mov bx,dx
	mov dl,al
	cmp dl,0h
	jnz tag4
	cmp si,0
	jz zero4

tag4:	
	mov si,1
	add dl,30h
	mov ah,2
	int 21h
zero4:
	mov ax,bx
	mov dx,0
	mov cx,1d
	div cx
	mov bx,dx
	mov dl,al
	cmp dl,0h
	jnz tag5
	cmp si,0
	jz zero5

tag5:
	add dl,30h
	mov ah,2
	int 21h
zero5:
	ret
hexde	endp

code ends
end start
